﻿using System.GunSystem;
using System.TimeSystem;
using Model;
using QFramework;

namespace Command
{
    public class ReloadCommand : AbstractCommand
    {
        protected override void OnExecute()
        {
            var currentGun = this.GetSystem<IGunSystem>().CurrentGun;

            var gunConfigItem = this.GetModel<IGunConfigModel>().GetItemByName(currentGun.Name.Value);

            // 需要的子弹数量
            var needBulletCount = gunConfigItem.BulletMaxCount - currentGun.BulletCountInGun.Value;

            if (needBulletCount > 0 && currentGun.BulletCountOutGun.Value > 0)
            {
                // 状态切换
                currentGun.GunState.Value = GunState.Reload;

                // 状态切回
                this.GetSystem<ITimeSystem>().AddDelayTask(gunConfigItem.ReloadSeconds, () =>
                {
                    if (currentGun.BulletCountOutGun.Value >= needBulletCount)
                    {
                        // 如果枪外子弹很充足
                        currentGun.BulletCountOutGun.Value -= needBulletCount;
                        currentGun.BulletCountInGun.Value += needBulletCount;
                    }
                    else
                    {
                        // 如果不充足，则全部填弹
                        currentGun.BulletCountInGun.Value += currentGun.BulletCountOutGun.Value;
                        currentGun.BulletCountOutGun.Value = 0;
                    }

                    currentGun.GunState.Value = GunState.Idle;
                });
            }
        }
    }
}